package com.mzy.datepackage.M2022.Feb.ele;

import com.google.common.collect.Lists;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Ives
 * * @date 2022/2/11
 */
public class demo {
  public static void main(String[] args) {
    /**
     * 统计list中元素的重复数量
     */
    List<String> words = Lists.newArrayList("a", "b", "c", "d", "a", "c");
    Map<String, Integer> countMap = new HashMap<String, Integer>();
    for (String word : words) {
      Integer count = countMap.get(word);
      count = (count == null) ? 1 : ++count;
      countMap.put(word, count);
    }
    countMap.forEach((k, v) -> System.out.println(k + ":" + v + "次"));
/**
 * result:
 * a:2
 * b:1
 * c:2
 * d:1
 */
    ArrayList<String> list = new ArrayList<>();
    list.add("111");
    list.add("111");
    list.add("111");
    list.add(null);

    List<Object> collect1 = list.stream().map(e -> Optional.ofNullable(e)).collect(Collectors.toList());
    System.out.println(collect1);
    Map<Optional<Object>, List<Object>> collect = list.stream().collect(Collectors.groupingBy(e -> Optional.ofNullable(e)));
    System.out.println(collect);
  }
}
